<template>
  <div class="front-page-container">
    <div class="front-page">
      <GoodsSourse :searchValue="pagination.searchValue" :goods="goods" @queryGoods="queryGoods"></GoodsSourse>
      <Pagination :pagination="pagination" @currentChange="currentChange"></Pagination>
    </div>
    <div class="right-info">
      <div class="free-btn">
        <img src="../assets/img/fabuxinxi2.png" @click="router.push('/addmessage/publishgoods')">
      </div>
      <div class="top-info">
        <div style="font-size:18px"><strong>近期热门信息</strong></div>
        <div class="item-info" v-for="(item,index) in hotGoods" :key="item.orderId">{{ index+1 }}、{{ item.title }}</div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { QueryOrdersAPI, GetHotGoodsByLimitAPI } from '../api/order'
import { useRouter} from 'vue-router'

const router = useRouter()


const goods = ref([])

const pagination = ref({
  size: 10,
  total: 0,
  page: 1,
  searchValue: ''
});


onMounted(() => {
  getGoods()
  getHotGoodsByLimit(10)
})

function getGoods() {
  QueryOrdersAPI(pagination.value)
  .then((res) => {
        if(res.code === 200&&res.msg!==null){
          goods.value = res.data.records
          pagination.value.total = res.data.total
          
        }
        if(res.code === 100&&res.msg!==null){
          ElMessage.error(res.msg)
        }

    })
}

const currentChange = (val: number) => {
  pagination.value.page = val
  getGoods()
}

const queryGoods = (val: string) => {
  pagination.value.searchValue = val
  getGoods()
}

const hotGoods = ref([
  {
    orderId: '',
    title: '',
  }
])

function getHotGoodsByLimit(limit: number){
  GetHotGoodsByLimitAPI({limit:limit})
  .then((res) => {
        if(res.code === 200&&res.msg!==null){
          hotGoods.value = res.data
          
        }
        if(res.code === 100&&res.msg!==null){
          ElMessage.error(res.msg)
        }

    })
}
</script>

<style lang="less" scoped>
.front-page-container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  width: 1100px;
  margin: 10px auto;
}

.front-page {
  width: 780px;
  margin: 0 auto;
}

.item-info {
  line-height: 25px;
  cursor: pointer;
  text-align: left;

  &:hover {
    color: #035D1C;
    text-decoration: underline;
  }
}

.right-info {
  padding: 10px 20px;
  height: 1000px;
  width: 300px;
  min-height: 360px;
  background-color: #fff;

  .free-btn {
    text-align: center;
    cursor: pointer;
  }

  .top-info {
    font-size: 12px;
    margin-top: 20px;
    text-align: left;
  }
}
</style>